package com.kehutong.payfees.admin;

import java.time.LocalDateTime;

import com.kehutong.common.util.Token;
import com.kehutong.payfees.util.JDBCUtil;
import org.coraframework.authz.HasPermission;
import org.coraframework.converter.Converter;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.orm.jdbc.execute.entity.FindPage;
import org.coraframework.orm.util.Enums;

import com.kehutong.payfees.entity.ChargeCallTarget;
import com.kehutong.payfees.entity.ChargeCallTask;
import com.kehutong.payfees.enums.BillCallMethod;
import com.kehutong.payfees.service.ChargeCallServiceImpl;

/**
 * 催缴任务模块
 */
@MvcAction
@ReqMapping("/admin/charge/call")
public class ChargeCallAction {
    @Inject
    private ChargeCallServiceImpl chargeCallService;
    @Inject
    private JdbcSession jdbcSession;

    /**
     * 催缴任务分页列表
     */
    @HasPermission("charge:call:view")
    @ReqMapping("/task/page")
    public Object pageTask(JSONObject req) {

        FindPage<ChargeCallTask> find = jdbcSession.findPage(ChargeCallTask.class)
                .eq(req, "billNo", "run")
                .order("configCallAt", "DESC")
                .page(req);

        JDBCUtil.between(req, find, "updateTime");

        Page<ChargeCallTask> page = find.exe();

        return Result.success(page);
    }

    /**
     * 催缴任务-发送对象分页列表
     */
    @HasPermission("charge:call:view")
    @ReqMapping("/task/target/page")
    public Object pageTaskTarget(JSONObject req) {
        Page<ChargeCallTarget> page = jdbcSession.findPage(ChargeCallTarget.class)
                .eq(req, "taskNo")
                .page(req)
                .exe();

        return Result.success(page);
    }

    @HasPermission("charge:call:edit")
    @ReqMapping("/task/update")
    public Object updateTask(JSONObject req, Token token) throws Exception{
        // 入参
        BillCallMethod callMethod = Enums.getEnum(BillCallMethod.class, req.getString("callMethod"));
        LocalDateTime configCallAt;
        if (BillCallMethod.TIMING == callMethod) {
            configCallAt = Converter.toLocalDateTime(req.get("configCallAt"));
            if (configCallAt.isBefore(LocalDateTime.now())) {
                return Result.error(0, "定时催缴时间不可早于当前");
            }
        } else if (BillCallMethod.IMMEDIATELY == callMethod) {
            configCallAt = LocalDateTime.now();
        } else {
            return Result.error(0, "未定义的催缴方式");
        }

        ChargeCallTask task = jdbcSession.findOne(ChargeCallTask.class)
                .eq(req, "id")
                .exe();

        if (task == null) {
            return Result.error(0, "不存在的催缴任务");
        }
        if (task.isRun()) {
            return Result.error(0, "已执行的催缴任务不可编辑");
        }

        task.setCallMethod(callMethod);
        task.setConfigCallAt(configCallAt);
        jdbcSession.updateById(task);

        // 刷新缴费任务催缴配置
        chargeCallService.refreshBillCallState(task.getBillNo());

        chargeCallService.afterCallTaskUpdate(task,token.getCompanyNo());

        return Result.success();
    }

    @HasPermission("charge:call:edit")
    @ReqMapping("/task/delete")
    public Object deleteTask(JSONObject req) {

        ChargeCallTask task = jdbcSession.findOne(ChargeCallTask.class)
                .eq(req, "id")
                .exe();

        if (task == null) {
            return Result.error(0, "不存在的催缴任务");
        }
        if (task.isRun()) {
            return Result.error(0, "已执行的催缴任务不可删除");
        }

        jdbcSession.deleteById(task);

        // 刷新缴费任务催缴配置
        chargeCallService.refreshBillCallState(task.getBillNo());

        return Result.success();
    }
}
